home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / OpenLinux 2.3 CD.iso / live / usr / lib / perl5 / 5.00502 / Tie / Array.pm next >
Encoding:
Perl POD Document  |  1999-08-11  |  6.4 KB  |  263 lines

  1. package Tie::Array;
  2. use vars qw($VERSION); 
  3. use strict;
  4. $VERSION = '1.00';
  5.  
  6. # Pod documentation after __END__ below.
  7.  
  8. sub DESTROY { }
  9. sub EXTEND  { }          
  10. sub UNSHIFT { shift->SPLICE(0,0,@_) }                 
  11. sub SHIFT   { shift->SPLICE(0,1) }                 
  12. sub CLEAR   { shift->STORESIZE(0) }
  13.  
  14. sub PUSH 
  15. {  
  16.  my $obj = shift;
  17.  my $i   = $obj->FETCHSIZE;
  18.  $obj->STORE($i++, shift) while (@_);
  19. }
  20.  
  21. sub POP 
  22. {
  23.  my $obj = shift;
  24.  my $newsize = $obj->FETCHSIZE - 1;
  25.  my $val;
  26.  if ($newsize >= 0) 
  27.   {
  28.    $val = $obj->FETCH($newsize);
  29.    $obj->STORESIZE($newsize);
  30.   }
  31.  $val;
  32. }          
  33.  
  34. sub SPLICE
  35. {
  36.  my $obj = shift;
  37.  my $sz  = $obj->FETCHSIZE;
  38.  my $off = (@_) ? shift : 0;
  39.  $off += $sz if ($off < 0);
  40.  my $len = (@_) ? shift : $sz - $off;
  41.  my @result;
  42.  for (my $i = 0; $i < $len; $i++)
  43.   {
  44.    push(@result,$obj->FETCH($off+$i));
  45.   }
  46.  if (@_ > $len)
  47.   {                          
  48.    # Move items up to make room
  49.    my $d = @_ - $len;
  50.    my $e = $off+$len;
  51.    $obj->EXTEND($sz+$d);
  52.    for (my $i=$sz-1; $i >= $e; $i--)
  53.     {
  54.      my $val = $obj->FETCH($i);
  55.      $obj->STORE($i+$d,$val);
  56.     }
  57.   }
  58.  elsif (@_ < $len)
  59.   {
  60.    # Move items down to close the gap 
  61.    my $d = $len - @_;
  62.    my $e = $off+$len;
  63.    for (my $i=$off+$len; $i < $sz; $i++)
  64.     {
  65.      my $val = $obj->FETCH($i);
  66.      $obj->STORE($i-$d,$val);
  67.     }
  68.    $obj->STORESIZE($sz-$d);
  69.   }
  70.  for (my $i=0; $i < @_; $i++)
  71.   {
  72.    $obj->STORE($off+$i,$_[$i]);
  73.   }
  74.  return @result;
  75.  
  76. package Tie::StdArray;
  77. use vars qw(@ISA);
  78. @ISA = 'Tie::Array';
  79.  
  80. sub TIEARRAY  { bless [], $_[0] }
  81. sub FETCHSIZE { scalar @{$_[0]} }             
  82. sub STORESIZE { $#{$_[0]} = $_[1]-1 }  
  83. sub STORE     { $_[0]->[$_[1]] = $_[2] }
  84. sub FETCH     { $_[0]->[$_[1]] }
  85. sub CLEAR     { @{$_[0]} = () }
  86. sub POP       { pop(@{$_[0]}) } 
  87. sub PUSH      { my $o = shift; push(@$o,@_) }
  88. sub SHIFT     { shift(@{$_[0]}) } 
  89. sub UNSHIFT   { my $o = shift; unshift(@$o,@_) } 
  90.  
  91. sub SPLICE
  92. {
  93.  my $ob  = shift;                    
  94.  my $sz  = $ob->FETCHSIZE;
  95.  my $off = @_ ? shift : 0;
  96.  $off   += $sz if $off < 0;
  97.  my $len = @_ ? shift : $sz-$off;
  98.  return splice(@$ob,$off,$len,@_);
  99. }
  100.  
  101. 1;
  102.  
  103. __END__
  104.  
  105. =head1 NAME
  106.  
  107. Tie::Array - base class for tied arrays
  108.  
  109. =head1 SYNOPSIS  
  110.  
  111.     package NewArray;
  112.     use Tie::Array;
  113.     @ISA = ('Tie::Array');
  114.                        
  115.     # mandatory methods
  116.     sub TIEARRAY { ... }  
  117.     sub FETCH { ... }     
  118.     sub FETCHSIZE { ... } 
  119.         
  120.     sub STORE { ... }        # mandatory if elements writeable
  121.     sub STORESIZE { ... }    # mandatory if elements can be added/deleted
  122.                                
  123.     # optional methods - for efficiency
  124.     sub CLEAR { ... }  
  125.     sub PUSH { ... } 
  126.     sub POP { ... } 
  127.     sub SHIFT { ... } 
  128.     sub UNSHIFT { ... } 
  129.     sub SPLICE { ... } 
  130.     sub EXTEND { ... } 
  131.     sub DESTROY { ... }
  132.  
  133.     package NewStdArray;
  134.     use Tie::Array;
  135.     
  136.     @ISA = ('Tie::StdArray');
  137.  
  138.     # all methods provided by default
  139.  
  140.     package main;
  141.  
  142.     $object = tie @somearray,Tie::NewArray;
  143.     $object = tie @somearray,Tie::StdArray;
  144.     $object = tie @somearray,Tie::NewStdArray;
  145.  
  146.  
  147.  
  148. =head1 DESCRIPTION       
  149.  
  150. This module provides methods for array-tying classes. See
  151. L<perltie> for a list of the functions required in order to tie an array
  152. to a package. The basic B<Tie::Array> package provides stub C<DELETE> 
  153. and C<EXTEND> methods, and implementations of C<PUSH>, C<POP>, C<SHIFT>, 
  154. C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>, 
  155. C<FETCHSIZE>, C<STORESIZE>.
  156.  
  157. The B<Tie::StdArray> package provides efficient methods required for tied arrays 
  158. which are implemented as blessed references to an "inner" perl array.
  159. It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly 
  160. like standard arrays, allowing for selective overloading of methods. 
  161.  
  162. For developers wishing to write their own tied arrays, the required methods
  163. are briefly defined below. See the L<perltie> section for more detailed
  164. descriptive, as well as example code:
  165.  
  166. =over 
  167.  
  168. =item TIEARRAY classname, LIST
  169.  
  170. The class method is invoked by the command C<tie @array, classname>. Associates
  171. an array instance with the specified class. C<LIST> would represent
  172. additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
  173. to complete the association. The method should return an object of a class which
  174. provides the methods below. 
  175.  
  176. =item STORE this, index, value
  177.  
  178. Store datum I<value> into I<index> for the tied array assoicated with
  179. object I<this>. If this makes the array larger then
  180. class's mapping of C<undef> should be returned for new positions.
  181.  
  182. =item FETCH this, index
  183.  
  184. Retrieve the datum in I<index> for the tied array assoicated with
  185. object I<this>.
  186.  
  187. =item FETCHSIZE this
  188.  
  189. Returns the total number of items in the tied array assoicated with
  190. object I<this>. (Equivalent to C<scalar(@array)>).
  191.  
  192. =item STORESIZE this, count
  193.  
  194. Sets the total number of items in the tied array assoicated with
  195. object I<this> to be I<count>. If this makes the array larger then
  196. class's mapping of C<undef> should be returned for new positions.
  197. If the array becomes smaller then entries beyond count should be
  198. deleted. 
  199.  
  200. =item EXTEND this, count
  201.  
  202. Informative call that array is likely to grow to have I<count> entries.
  203. Can be used to optimize allocation. This method need do nothing.
  204.  
  205. =item CLEAR this
  206.  
  207. Clear (remove, delete, ...) all values from the tied array assoicated with
  208. object I<this>.
  209.  
  210. =item DESTROY this
  211.  
  212. Normal object destructor method.
  213.  
  214. =item PUSH this, LIST 
  215.  
  216. Append elements of LIST to the array.
  217.  
  218. =item POP this
  219.  
  220. Remove last element of the array and return it.
  221.  
  222. =item SHIFT this
  223.  
  224. Remove the first element of the array (shifting other elements down)
  225. and return it.
  226.  
  227. =item UNSHIFT this, LIST 
  228.  
  229. Insert LIST elements at the begining of the array, moving existing elements
  230. up to make room.
  231.  
  232. =item SPLICE this, offset, length, LIST
  233.  
  234. Perform the equivalent of C<splice> on the array. 
  235.  
  236. I<offset> is optional and defaults to zero, negative values count back 
  237. from the end of the array. 
  238.  
  239. I<length> is optional and defaults to rest of the array.
  240.  
  241. I<LIST> may be empty.
  242.  
  243. Returns a list of the original I<length> elements at I<offset>.
  244.  
  245. =back
  246.  
  247. =head1 CAVEATS
  248.  
  249. There is no support at present for tied @ISA. There is a potential conflict 
  250. between magic entries needed to notice setting of @ISA, and those needed to
  251. implement 'tie'.   
  252.  
  253. Very little consideration has been given to the behaviour of tied arrays
  254. when C<$[> is not default value of zero.
  255.  
  256. =head1 AUTHOR 
  257.  
  258. Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>
  259.  
  260. =cut 
  261.  
  262.